// pages/add/add.js
var Bmob = require("../../utils/Bmob-2.2.0.min.js")
Bmob.initialize("a25cd3ee1411fbed", "999423");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag1: "",
    flag2: "",
    option: {
      //value: "925b70a9f8"
    },
    calendar: "YYYY-MM-DD",
    time: "HH:MM",
    havel: "",
    haver: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // console.log("onload")
    var id = wx.getStorageSync("currentId")
    wx.removeStorageSync("currentId")
    console.log("finess")
    console.log(id)
    if (id != "") {
      const query = Bmob.Query('Todo');
      query.get(id).then(res => {
        console.log(res)
        this.setData({
          title: res.title,
          // date: res.date,
          calendar: res.date.split(" ")[0],
          time: res.date.split(" ")[1],
          ps: res.ps,
          havel: "1",
          haver: "1",
        })
      }).catch(err => {
        console.log(err)
      })
    } else {
      console.log("newtodo")
      this.setData({
        title: "",
        ps: "",
        calendar: "YYYY-MM-DD",
        time: "HH:MM",
        havel: "",
        haver: ""
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 点击单个Item
   */
  select: function() {
    if (!this.data.flag1) {
      this.setData({
        flag1: 2
      });
    } else {
      this.setData({
        flag1: ""
      });
    }
  },

  /**
   * 收藏
   */
  love: function() {
    if (!this.data.flag2) {
      this.setData({
        flag2: 2
      });
    } else {
      this.setData({
        flag2: ""
      });
    }
  },

  /**
   * 表单提交
   */
  formSubmit: function(e) {
    wx.showLoading({
      title: '保存...',
    })
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    const query = Bmob.Query('Todo');
    query.set("title", e.detail.value.things)
    query.set("date", e.detail.value.calendar + " " + e.detail.value.time)
    query.set("ps", e.detail.value.ps)
    query.set("userId", wx.getStorageSync('usedata').useId)
    query.save().then(res => {
      console.log(res)
      wx.hideLoading()
      wx.showToast({
        title: '保存成功！',
        icon: 'success',
        duration: 1000,
        mask: true,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
      wx.switchTab({
        url: '/pages/homepage/homepage',
      })
    }).catch(err => {
      console.log(err)
    })
  },

  /**
   * 回退首页 
   * */
  comeback: function() {
    wx.switchTab({
      url: '../homepage/homepage',
    })
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      calendar: e.detail.value,
      havel: "1"
    })
  },
  bindTimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value,
      haver: "1"
    })
  },
})